package future;

import java.util.concurrent.CompletableFuture;

/**
 * ComplitableFuture
 * supplyAsync有返回值 runAsync无返回值
 * executor线程池
 * thenCombine汇聚关系
 * 串行
 * thenApply thenAccept thenCompose
 * 不get阻塞主线程 任务就无了
 *
 * 参数类型为 Function<T,R>
 * redicate<T>——接收 T 并返回 boolean
 * Consumer<T>——接收 T，不返回值
 * Function<T, R>——接收 T，返回 R
 * Supplier<T>——提供 T 对象（例如工厂），不接收值
 *
 * 2020-11-06 14:34
 *
 */
public class ComplitableFuture {
    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        CompletableFuture<String> f0 = CompletableFuture
                .supplyAsync(() -> {
                    try {
                        Thread.sleep(2000);
                    } catch (Exception e) {

                    }
                    return "I have completed";
                })
                .thenApply(s -> {
                    System.out.println(s);
                    return s + "2s later";
                });

        //参数为supply 不提供参数 提供返回值
        CompletableFuture<String> f1 = CompletableFuture
                .supplyAsync(() -> "sunyuxiangde")
                .thenApply(s ->
                {
                    try {
                        Thread.sleep(2500);
                    } catch (Exception e) {

                    }
                    return s + "qq shi:";
                });


        System.out.println(f0.get());
        System.out.println(f1.get());
        long end = System.currentTimeMillis();
        System.out.println("time is " + (end - start) / 1000.0);
    }
}
